/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.gen.web;

import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.gen.dao.GenTableColumnDao;
import com.thinkgem.jeesite.modules.gen.entity.GenTable;
import com.thinkgem.jeesite.modules.gen.entity.GenTableColumn;
import com.thinkgem.jeesite.modules.gen.entity.GenTableJoinColumn;
import com.thinkgem.jeesite.modules.gen.service.GenTableJoinColumnService;
import com.thinkgem.jeesite.modules.gen.service.GenTableService;
import com.thinkgem.jeesite.modules.gen.util.GenUtils;
import com.thinkgem.jeesite.modules.gen.util.JsonUtil;
import com.thinkgem.jeesite.modules.gen.util.TableColumnTableJoinColumnPackage;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务表Controller
 *
 * @author ThinkGem
 * @version 2013-10-15
 */
@Controller
@RequestMapping(value = "${adminPath}/gen/genJoinColumn")
public class GenJoinColumnController extends BaseController {

    @Autowired
    private GenTableService genTableService;
    @Autowired
    private GenTableJoinColumnService genTableJoinColumnService;
    @Autowired
    private GenTableColumnDao genTableColumnDao;

    @ModelAttribute
    public GenTableColumn get(@RequestParam(required = false) String id) {
        if (StringUtils.isNotBlank(id)) {
            return genTableColumnDao.get(id);
        } else {
            return new GenTableColumn();
        }
    }

    @RequiresPermissions("gen:genTable:view")
    @RequestMapping(value = "setJoinColumn")
    public String setJoinColumn(GenTableColumn genTableColumn, Model model) {
        genTableColumn = genTableColumnDao.get(genTableColumn);
        model.addAttribute("genTableColumn", genTableColumn);
        model.addAttribute("config", GenUtils.getConfig());

        // 获取物理表列表
        List<GenTable> tableList = genTableService.findTableListFormDb(new GenTable());
        model.addAttribute("tableList", tableList);
        //所有的列
        List<GenTableJoinColumn> allColumns = genTableJoinColumnService.getTableColumnFormDb(genTableColumn);
        //上一次保存的列
        List<GenTableJoinColumn> columns = genTableJoinColumnService.getList(genTableColumn.getId());
        //覆盖
        List<GenTableJoinColumn> finalColumns = handle(allColumns, columns);

        model.addAttribute("columns", finalColumns);

        return "modules/gen/setJoinColumn";
    }

    private List<GenTableJoinColumn> handle(List<GenTableJoinColumn> allColumns, List<GenTableJoinColumn> columns) {
        List<GenTableJoinColumn> finalColumns = new ArrayList<>();
        if (allColumns == null) {
            return finalColumns;
        }
        for (GenTableJoinColumn item : allColumns) {
            boolean in = false;
            GenTableJoinColumn iii = null;
            for (GenTableJoinColumn item2 : columns) {
                if (item2 != null && item2.getName() != null && item2.getName().equals(item.getName())) {
                    in = true;
                    iii = item2;
                    break;
                }
            }
            if (in) {
                iii.setChecked(true);
                finalColumns.add(iii);
            } else {
                finalColumns.add(item);
            }
        }

        return finalColumns;
    }

    @RequiresPermissions("gen:genTable:view")
    @RequestMapping(value = "ajaxPrimaryTableColumn")
    @ResponseBody
    public String ajaxPrimaryTableColumn(GenTableColumn genTableColumn, Model model) {
        List<GenTableJoinColumn> columns = genTableJoinColumnService.getTableColumnFormDb(genTableColumn);
        return JsonUtil.toJSONString(columns);
    }


    @RequiresPermissions("gen:genTable:view")
    @RequestMapping(value = "save")
    @ResponseBody
    public Boolean save(@RequestBody TableColumnTableJoinColumnPackage tableColumnTableJoinColumnPackage, Model model) {
        boolean result = genTableJoinColumnService.save(tableColumnTableJoinColumnPackage);
        return result;
    }

    @RequiresPermissions("gen:genTable:view")
    @RequestMapping(value = "viewJoinColumn")
    public String viewJoinColumn(GenTableColumn genTableColumn, Model model) {
        genTableColumn = genTableColumnDao.get(genTableColumn);
        model.addAttribute("genTableColumn", genTableColumn);
        model.addAttribute("config", GenUtils.getConfig());

        List<GenTableJoinColumn> genTableJoinColumns = genTableJoinColumnService.getList(genTableColumn.getId());


        if (genTableJoinColumns != null && !genTableJoinColumns.isEmpty()) {
            Map<String, Object> result = new HashMap();
            for (GenTableJoinColumn column : genTableJoinColumns) {
                result.put(column.getJavaField(), column.getComments());
            }
            String json = JsonUtil.toJSONString(result);
            System.out.println("var obj = " + json + " ; ");
        }

        model.addAttribute("genTableJoinColumns", genTableJoinColumns);

        return "modules/gen/viewJoinColumn";
    }
}
